package com.example.demo.controller;

import com.example.demo.Po.LyricPo;
import com.example.demo.Po.Lyrics;
import com.example.demo.Po.MusicAddPo;
import com.example.demo.db.Lyric;
import com.example.demo.db.Result;
import com.example.demo.mapper.MusicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
@Validated
@RequestMapping("/user")
public class UserController {
    @Autowired
    private MusicMapper mapper;

    @GetMapping("/musicList")
    public Result selectAll() {
        List<String> name = new ArrayList<>();
        name = mapper.select();
        return Result.success(name);
    }
    @PostMapping("/song")
    public Result selectSong(@RequestBody LyricPo lyricPo) {
        String songUrl = "http://localhost:8080/static/music/";
        String imgUrl = "http://localhost:8080/static/music/";
        String lirc = "一个人挺好-孟颖.mp3";
        String title = "";
        if (lyricPo.getName() != null && lyricPo.getName().length() > 1) {
            lirc = lyricPo.getName();
        }
        List<LyricPo> list = mapper.selectMusic(lirc);
        System.out.println(list);
        if (list.size() > 1) {
            lirc = list.get(0).getData();
            songUrl += list.get(0).getName();
            title = list.get(0).getName().replaceAll(".mp3","");
            imgUrl += list.get(0).getImgUrl();
        } else if (list.size() == 0) {
            lirc = "一个人挺好-孟颖.mp3";
            list = mapper.selectMusic(lirc);
            lirc = list.get(0).getData();
            songUrl += list.get(0).getName();
            title = list.get(0).getName().replaceAll(".mp3","");
            imgUrl += list.get(0).getImgUrl();
        } else {
            lirc = list.get(0).getData();
            songUrl += list.get(0).getName();
            title = list.get(0).getName().replaceAll(".mp3","");
            imgUrl += list.get(0).getImgUrl();
        }
        String[] arrayList = lirc.split("\n");
        List<Lyric> lyrics = new ArrayList<>();
        for (int i = 0; i < arrayList.length; i++) {
            String[] array = arrayList[i].split("]");
            int time = createTime(array[0].substring(1));
            String text = array[1];
            Lyric lyric = new Lyric();
            lyric.setTime(time);
            lyric.setText(text);
            lyrics.add(lyric);
        }
        Lyrics lyrics1 = new Lyrics();
        lyrics1.setSongUrl(songUrl);
        lyrics1.setList(lyrics);
        lyrics1.setTitle(title.split("-")[0]);
        lyrics1.setArtist(title.split("-")[1]);
        lyrics1.setImgUrl(imgUrl);
        return Result.success(lyrics1);
    }
    private int createTime(String time) {
        // 提取分和秒
        String[] times = time.split(":");
        int minutes = Integer.parseInt(times[0]); // 将分钟部分转换为整数
        String[] secondsAndMillis = times[1].split("\\.");
        int seconds = Integer.parseInt(secondsAndMillis[0]); // 将秒部分转换为整数
        // 计算总时间（秒）
        int totalTimeInSeconds = minutes * 60 + seconds;
        return totalTimeInSeconds;
    }
    @PostMapping("/musicAdd")
    public Result musicAdd(@RequestBody MusicAddPo musicAddPo) {
        System.out.println(musicAddPo);
        if (musicAddPo.getMusicName().length()>1 && musicAddPo.getText().length()>1 && musicAddPo.getFile().length()>1){
            if (musicAddPo.getImgUrl()==null && musicAddPo.getImgUrl().length()<2){
                musicAddPo.setImgUrl("181750LAK1m.jpg");
            }
            mapper.insert(musicAddPo);
            return Result.success();
        }
        return Result.error("添加失败");
    }
    //修改 文件上传路径，根据自己的文件路径进行修改，
    public static final String UPLOAD_PATH = "D:\\cad\\vue\\itcast-tanhua\\demo\\src\\main\\resources\\static\\music\\";
    //单文件或者小文件上传
    @PostMapping("/musicFileUpload")
    public Result creates(@RequestBody MultipartFile file) throws IOException {
        String ext = StringUtils.getFilename(file.getOriginalFilename());
        File dstFile = new File(UPLOAD_PATH, String.format("%s",ext));
        file.transferTo(dstFile);
        return Result.success(ext);
    }
}
